package com.jiangnanxiaoguan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jiangnanxiaoguan.dto.SetmealDto;
import com.jiangnanxiaoguan.entity.Setmeal;
import com.jiangnanxiaoguan.entity.SetmealDish;
import com.jiangnanxiaoguan.exception.BusinessException;
import com.jiangnanxiaoguan.mapper.SetmealMapper;
import com.jiangnanxiaoguan.service.SetmealDishService;
import com.jiangnanxiaoguan.service.SetmealService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 套餐 服务实现类
 * </p>
 *
 * @author 不愿透露姓名的狂热者
 * @since 2022-09-11
 */
@Slf4j
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;


    @Transactional
    @Override
    public boolean saveWithSetmealDishes(SetmealDto setmealDto) {
        // 套餐表
        save(setmealDto);

        // 套餐菜品关系表
        Long setmealId = setmealDto.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        // 每个套餐菜品都加上套餐id
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(String.valueOf(setmealId));
        }
        setmealDto.setSetmealDishes(setmealDishes);

        return setmealDishService.saveBatch(setmealDishes);
    }


    @Transactional
    @Override
    public boolean deleteWithSetmealDishes(List<Long> ids) {
        // 先看套餐是否是起售状态
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.in(Setmeal::getId, ids).eq(Setmeal::getStatus, 1);
        if (count(setmealLambdaQueryWrapper) > 0) {
            throw new BusinessException("请先 禁售套餐 再删除。");
        }

        // 删除套餐记录
        removeByIds(ids);

        // 删除套餐菜品关联关系记录
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);
        return setmealDishService.remove(setmealDishLambdaQueryWrapper);
    }


    @Transactional
    @Override
    public void updateWithSetmealDishes(SetmealDto setmealDto) {
        // 1、更新套餐信息。
        updateById(setmealDto);

        Long setmealId = setmealDto.getId();
        // 2、将所有套餐菜品信息追加关联的套餐id。
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(String.valueOf(setmealId)));

        // 3、先根据套餐id清除原套餐菜品数据。
        LambdaUpdateWrapper<SetmealDish> setmealDishLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        setmealDishLambdaUpdateWrapper.eq(SetmealDish::getSetmealId, setmealId);
        setmealDishService.remove(setmealDishLambdaUpdateWrapper);

        // 4、添加现修改后的套餐菜品数据。
        setmealDishService.saveBatch(setmealDishes);
    }
}
